﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using HYBatchPrinter.Utils;

namespace HYBatchPrinter
{
	public partial class ViewSheetSetMgrForm : System.Windows.Forms.Form
	{
		public ViewSheetSetMgrForm(ExternalCommandData commandData)
		{
			this.InitializeComponent();
			this.m_doc = commandData.Application.ActiveUIDocument.Document;
			this.m_viewSheet = new ViewSheets(this.m_doc);
		}

		private void InitData()
		{
			this.InitSheetNameDic();
			this.InitSheetSetNameDic();
		}

		private void InitSheetNameDic()
		{
			if (this.m_SheetNameToView.Count == 0)
			{
				foreach (Autodesk.Revit.DB.View view in this.m_viewSheet.AvailableViewSheetSet(VisibleType.VT_SheetOnly))
				{
					ViewSheet viewSheet = view as ViewSheet;
					if (viewSheet != null)
					{
						string viewSheetName = ViewSheetManager.GetViewSheetName(viewSheet);
						this.m_SheetNameToView.Add(viewSheetName, viewSheet);
					}
				}
			}
		}

		private void InitSheetSetNameDic()
		{
			if (this.m_SetNameToSetObj.Count == 0)
			{
				foreach (ViewSheetSet viewSheetSet in ViewSheetManager.GetCurDocSheetSet(this.m_doc))
				{
					this.m_SetNameToSetObj.Add(viewSheetSet.Name, viewSheetSet);
				}
			}
		}

		private void InitViewTreeView(TreeView trView)
		{
			foreach (KeyValuePair<string, View> keyValuePair in this.m_SheetNameToView)
			{
				string key = keyValuePair.Key;
				ViewSheet viewSheet = keyValuePair.Value as ViewSheet;
				TreeNode treeNode = new TreeNode(key);
				if (viewSheet != null)
				{
					foreach (string text in ViewSheetManager.GetSheetPortsNames(viewSheet))
					{
						treeNode.Nodes.Add(text);
					}
				}
				trView.Nodes.Add(treeNode);
			}
			trView.Sort();
			this.SetDefTreeViewNode(trView);
		}

		private void InitSheetCombo(System.Windows.Forms.ComboBox comboBox)
		{
			foreach (KeyValuePair<string, ViewSheetSet> keyValuePair in this.m_SetNameToSetObj)
			{
				comboBox.Items.Add(keyValuePair.Key);
			}
			this.comboSheetSet.SelectedIndex = this.comboSheetSet.Items.Count - 1;
		}

		private int GetStrPixelLength(string str)
		{
			return (int)base.CreateGraphics().MeasureString(str, this.Font).Width;
		}

		private List<string> GetListBoxSelItems(ListBox listbox)
		{
			List<string> list = new List<string>();
			foreach (object obj in listbox.SelectedItems)
			{
				string text = (string)obj;
				if (!string.IsNullOrEmpty(text))
				{
					list.Add(text);
				}
			}
			return list;
		}

		private void SetComboBoxMaxLength(System.Windows.Forms.ComboBox comboCtrl)
		{
			List<string> list = new List<string>();
			foreach (object obj in comboCtrl.Items)
			{
				string text = (string)obj;
				if (text != null)
				{
					list.Add(text);
				}
			}
			if (list.Count > 0)
			{
				int num = 0;
				foreach (string text2 in list)
				{
					int num2 = (int)base.CreateGraphics().MeasureString(text2, this.Font).Width;
					if (num2 > num)
					{
						num = num2;
					}
				}
				if (num != 0)
				{
					comboCtrl.DropDownWidth = num;
				}
			}
		}

		private void ReSetSheetViewName(ListView lvSheetViews, ViewSheetSet viewSheetSet)
		{
			lvSheetViews.Clear();
			foreach (object obj in viewSheetSet.Views)
			{
				ViewSheet viewSheet = obj as ViewSheet;
				if (viewSheet != null)
				{
					string viewSheetName = ViewSheetManager.GetViewSheetName(viewSheet);
					ListViewItem listViewItem = new ListViewItem(viewSheetName);
					listViewItem.SubItems.Add(viewSheetName);
					listViewItem.SubItems.Add(viewSheetName);
					if (this.GetStrPixelLength(viewSheetName) > lvSheetViews.Width)
					{
						listViewItem.ToolTipText = viewSheetName;
					}
					lvSheetViews.Items.Add(listViewItem);
				}
			}
			lvSheetViews.Sort();
		}

		private void ReSetSheetViewName(ListBox listBox, ViewSheetSet viewSheetSet)
		{
			listBox.Items.Clear();
			foreach (object obj in viewSheetSet.Views)
			{
				Autodesk.Revit.DB.View view = (Autodesk.Revit.DB.View)obj;
				ViewSheet viewSheet = this.m_doc.GetElement(view.Id) as ViewSheet;
				if (viewSheet != null)
				{
					string viewSheetName = ViewSheetManager.GetViewSheetName(viewSheet);
					if (!string.IsNullOrEmpty(viewSheetName))
					{
						listBox.Items.Add(viewSheetName);
					}
				}
				this.SetDefListBoxItem(listBox);
			}
		}

		private void SetDefTreeViewNode(TreeView trView)
		{
			if (trView != null && trView.Nodes.Count > 0)
			{
				trView.SelectedNode = trView.Nodes[0];
				CtrlShiftSelTreeView ctrlShiftSelTreeView = trView as CtrlShiftSelTreeView;
				if (ctrlShiftSelTreeView != null)
				{
					TreeNode selectedNode = trView.SelectedNode;
					if (selectedNode != null)
					{
						ctrlShiftSelTreeView.SelectedNodeList.Clear();
						ctrlShiftSelTreeView.SelectedNodeList.Add(selectedNode);
						ctrlShiftSelTreeView.Invalidate();
						ctrlShiftSelTreeView.LastSelectNode = selectedNode;
					}
				}
				trView.Select();
			}
		}

		private void SetDefListBoxItem(ListBox listBox)
		{
			if (listBox != null && listBox.Items.Count > 0)
			{
				listBox.SelectedItems.Clear();
				listBox.SelectedIndex = 0;
			}
		}

		private void ReSetTreeView(TreeView trView)
		{
			trView.Nodes.Clear();
			foreach (KeyValuePair<string, View> keyValuePair in this.m_SheetNameToView)
			{
				string key = keyValuePair.Key;
				if (!this.HasItemName(this.lBoxViewSheetSet, key))
				{
					ViewSheet viewSheet = keyValuePair.Value as ViewSheet;
					TreeNode treeNode = new TreeNode(key);
					if (viewSheet != null)
					{
						foreach (string text in ViewSheetManager.GetSheetPortsNames(viewSheet))
						{
							treeNode.Nodes.Add(text);
						}
					}
					trView.Nodes.Add(treeNode);
				}
			}
			trView.Sort();
			this.SetDefTreeViewNode(trView);
		}

		private void ReSetSheetCombo(System.Windows.Forms.ComboBox comboBox, string sCurSheetName)
		{
			comboBox.Items.Clear();
			foreach (KeyValuePair<string, ViewSheetSet> keyValuePair in this.m_SetNameToSetObj)
			{
				comboBox.Items.Add(keyValuePair.Key);
			}
			if (comboBox.Items.Count > 0)
			{
				int num = 0;
				foreach (KeyValuePair<string, ViewSheetSet> keyValuePair2 in this.m_SetNameToSetObj)
				{
					if (keyValuePair2.Key.Equals(sCurSheetName))
					{
						break;
					}
					num++;
				}
				this.comboSheetSet.SelectedIndex = num;
			}
		}

		private bool HasItemName(ListView lvView, string sViewName)
		{
			bool result = false;
			IEnumerator enumerator = this.lvSheetViews.Items.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((ListViewItem)enumerator.Current).Text.Equals(sViewName))
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		private bool HasItemName(ListBox lvBoxView, string sViewName)
		{
			bool result = false;
			if (-1 != lvBoxView.FindStringExact(sViewName, 0))
			{
				result = true;
			}
			return result;
		}

		private bool HasNodeName(TreeView trView, string sSheetName)
		{
			bool result = false;
			IEnumerator enumerator = trView.Nodes.GetEnumerator();
			{
				while (enumerator.MoveNext())
				{
					if (((TreeNode)enumerator.Current).Name.Equals(sSheetName))
					{
						result = true;
						break;
					}
				}
			}
			return result;
		}

		private void AddOneNodeToViewSheetSet(TreeNode Node, string sSheetName)
		{
			string text = Node.Text;
			if (!this.HasItemName(this.lBoxViewSheetSet, text))
			{
				this.lBoxViewSheetSet.Items.Add(text);
				this.trViewSheet.Nodes.Remove(Node);
				ViewSheet viewSheetByName = this.GetViewSheetByName(text);
				ViewSheetSet viewSheetSetByName = this.GetViewSheetSetByName(sSheetName);
				ViewSheetManager.AddOneViewSheetToSet(viewSheetSetByName, viewSheetByName);
				this.m_viewSheet.ChangeCurrentViewSheetSet(viewSheetSetByName.Views);
			}
		}

		private void AddNodesToViewSheetSet(List<TreeNode> nodes, string sSheetName)
		{
			List<TreeNode> list = new List<TreeNode>();
			foreach (TreeNode treeNode in nodes)
			{
				if (treeNode != null)
				{
					string text = treeNode.Text;
					if (!this.HasItemName(this.lBoxViewSheetSet, text))
					{
						this.lBoxViewSheetSet.Items.Add(text);
						this.trViewSheet.Nodes.Remove(treeNode);
						list.Add(treeNode);
					}
				}
			}
			if (list.Count > 0)
			{
				ViewSheetSet viewSheetSetByName = this.GetViewSheetSetByName(sSheetName);
				foreach (TreeNode treeNode2 in list)
				{
					string text2 = treeNode2.Text;
					ViewSheet viewSheetByName = this.GetViewSheetByName(text2);
					ViewSheetManager.AddOneViewSheetToSet(viewSheetSetByName, viewSheetByName);
				}
				this.m_viewSheet.ChangeCurrentViewSheetSet(viewSheetSetByName.Views);
			}
		}

		private void DelOneItemFromViewSheetSet(ListViewItem item, string sSheetName)
		{
			string text = item.Text;
			if (!this.HasNodeName(this.trViewSheet, text))
			{
				TreeNode treeNode = this.CreateViewSheetNode(text);
				if (treeNode != null)
				{
					this.trViewSheet.Nodes.Add(treeNode);
					this.lvSheetViews.Items.Remove(item);
					ViewSheet viewSheetByName = this.GetViewSheetByName(text);
					ViewSheetSet viewSheetSetByName = this.GetViewSheetSetByName(sSheetName);
					ViewSheetManager.DelViewSheetFromSet(viewSheetSetByName, new List<ViewSheet>
					{
						viewSheetByName
					});
					this.m_viewSheet.ChangeCurrentViewSheetSet(viewSheetSetByName.Views);
				}
			}
		}

		private void DelItemsFromViewSheetSet(List<string> items, string sSheetName)
		{
			List<string> list = new List<string>();
			foreach (string text in items)
			{
				if (text != null)
				{
					string text2 = text;
					if (!this.HasNodeName(this.trViewSheet, text2))
					{
						TreeNode treeNode = this.CreateViewSheetNode(text2);
						if (treeNode != null)
						{
							this.trViewSheet.Nodes.Add(treeNode);
							this.lBoxViewSheetSet.Items.Remove(text);
							list.Add(text2);
						}
					}
				}
			}
			if (list.Count > 0)
			{
				ViewSheetSet viewSheetSetByName = this.GetViewSheetSetByName(sSheetName);
				List<ViewSheet> list2 = new List<ViewSheet>();
				foreach (string text3 in list)
				{
					if (!string.IsNullOrEmpty(text3))
					{
						ViewSheet viewSheetByName = this.GetViewSheetByName(text3);
						if (viewSheetByName != null)
						{
							list2.Add(viewSheetByName);
						}
					}
				}
				ViewSheetManager.DelViewSheetFromSet(viewSheetSetByName, list2);
				this.m_viewSheet.ChangeCurrentViewSheetSet(viewSheetSetByName.Views);
			}
		}

		private ViewSheet GetViewSheetByName(string sSheetName)
		{
			return this.m_SheetNameToView[sSheetName] as ViewSheet;
		}

		private ViewSheetSet GetViewSheetSetByName(string sSetName)
		{
			return this.m_SetNameToSetObj[sSetName];
		}

		private TreeNode CreateViewSheetNode(string sSheetName)
		{
			TreeNode treeNode = null;
			if (this.m_SheetNameToView.ContainsKey(sSheetName))
			{
				treeNode = new TreeNode(sSheetName);
				ViewSheet viewSheet = this.m_SheetNameToView[sSheetName] as ViewSheet;
				if (viewSheet != null)
				{
					foreach (string text in ViewSheetManager.GetSheetPortsNames(viewSheet))
					{
						treeNode.Nodes.Add(text);
					}
				}
			}
			return treeNode;
		}

		private void ViewSheetSetMgrForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			this.trViewSheet.IsMultiSelect = true;
			this.InitData();
			this.InitViewTreeView(this.trViewSheet);
			this.InitSheetCombo(this.comboSheetSet);
		}

		private void btnAddToRight_Click(object sender, EventArgs e)
		{
			if (this.trViewSheet.Nodes.Count > 0 && this.trViewSheet.SelectedNode == null && this.trViewSheet.SelectedNodeList.Count == 0)
			{
				this.trViewSheet.SelectedNode = this.trViewSheet.Nodes[0];
			}
			TreeNode selectedNode = this.trViewSheet.SelectedNode;
			if (selectedNode == null)
			{
				YJKMessageBox.Show("请在 图纸 中至少选择一个图纸");
				return;
			}
			if (selectedNode.Level != 0)
			{
				YJKMessageBox.Show("请在 图纸 中至少选择一个图纸");
				return;
			}
			string text = this.comboSheetSet.Text;
			if (string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text))
			{
				YJKMessageBox.Show("添加失败，图纸集名称不存在");
				return;
			}
			if (this.trViewSheet.SelectedNodeList.Count == 0)
			{
				this.AddOneNodeToViewSheetSet(selectedNode, text);
				this.trViewSheet.SelectedNodeList.Clear();
			}
			else
			{
				List<TreeNode> list = new List<TreeNode>();
				foreach (TreeNode treeNode in this.trViewSheet.SelectedNodeList)
				{
					if (treeNode.Level == 0)
					{
						list.Add(treeNode);
					}
				}
				this.AddNodesToViewSheetSet(list, text);
				this.trViewSheet.SelectedNodeList.Clear();
			}
			this.trViewSheet.Sort();
			this.SetDefTreeViewNode(this.trViewSheet);
			this.SetDefListBoxItem(this.lBoxViewSheetSet);
		}

		private void btnAddToLeft_Click(object sender, EventArgs e)
		{
			if (this.lBoxViewSheetSet.SelectedItems.Count == 0)
			{
				YJKMessageBox.Show("请在 图纸集 中选择一个图纸项");
				return;
			}
			List<string> listBoxSelItems = this.GetListBoxSelItems(this.lBoxViewSheetSet);
			this.DelItemsFromViewSheetSet(listBoxSelItems, this.comboSheetSet.Text);
			this.trViewSheet.Sort();
			this.SetDefTreeViewNode(this.trViewSheet);
			this.SetDefListBoxItem(this.lBoxViewSheetSet);
		}

		private void btnAddAllToRight_Click(object sender, EventArgs e)
		{
			string text = this.comboSheetSet.Text;
			if (string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text))
			{
				YJKMessageBox.Show("添加失败，图纸集名称不存在");
				return;
			}
			List<TreeNode> list = new List<TreeNode>();
			foreach (object obj in this.trViewSheet.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode != null)
				{
					TreeNode treeNode2 = treeNode.Clone() as TreeNode;
					if (treeNode2 != null)
					{
						list.Add(treeNode2);
					}
				}
			}
			if (list.Count > 0)
			{
				this.AddNodesToViewSheetSet(list, text);
				this.trViewSheet.Nodes.Clear();
				this.trViewSheet.Sort();
				this.SetDefListBoxItem(this.lBoxViewSheetSet);
			}
		}

		private void btnAddAllToLeft_Click(object sender, EventArgs e)
		{
			if (this.lBoxViewSheetSet.Items.Count == 0)
			{
				YJKMessageBox.Show("没有可以移除的图纸");
				return;
			}
			if (this.lBoxViewSheetSet.Items.Count > 0)
			{
				List<string> list = new List<string>();
				foreach (object obj in this.lBoxViewSheetSet.Items)
				{
					string text = (string)obj;
					if (!string.IsNullOrEmpty(text))
					{
						list.Add(text);
					}
				}
				this.DelItemsFromViewSheetSet(list, this.comboSheetSet.Text);
				this.trViewSheet.Sort();
				this.SetDefTreeViewNode(this.trViewSheet);
			}
		}

		private void btnDel_Click(object sender, EventArgs e)
		{
			if (this.comboSheetSet.Items.Count == 1)
			{
				YJKMessageBox.Show("删除失败， 最后一个图纸集");
				return;
			}
			int selectedIndex = this.comboSheetSet.SelectedIndex;
			if (-1 != selectedIndex)
			{
				this.comboSheetSet.Items.RemoveAt(this.comboSheetSet.SelectedIndex);
				this.m_viewSheet.Delete();
				int selectedIndex2 = this.comboSheetSet.Items.Count - 1;
				this.comboSheetSet.SelectedIndex = selectedIndex2;
			}
		}

		private void btnReName_Click(object sender, EventArgs e)
		{
			RenameViewSheetSetForm renameViewSheetSetForm = new RenameViewSheetSetForm(this.m_viewSheet);
			if (DialogResult.OK == renameViewSheetSetForm.ShowDialog())
			{
				string newSheetName = renameViewSheetSetForm.GetNewSheetName();
				this.m_SetNameToSetObj.Clear();
				this.InitSheetSetNameDic();
				this.ReSetSheetCombo(this.comboSheetSet, newSheetName);
			}
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			NewViewSheetSetForm newViewSheetSetForm = new NewViewSheetSetForm(this.m_viewSheet);
			if (DialogResult.OK == newViewSheetSetForm.ShowDialog())
			{
				string newSheetName = newViewSheetSetForm.GetNewSheetName();
				this.m_SetNameToSetObj.Clear();
				this.InitSheetSetNameDic();
				this.ReSetSheetCombo(this.comboSheetSet, newSheetName);
			}
		}

		private void lvSheetViews_MouseMove(object sender, MouseEventArgs e)
		{
		}

		private void lvSheetViews_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
		{
		}

		private void comboSheetSet_SelectedIndexChanged(object sender, EventArgs e)
		{
			string text = this.comboSheetSet.Text;
			if (!string.IsNullOrEmpty(text) && !string.IsNullOrWhiteSpace(text))
			{
				ViewSheetSet viewSheetSet = this.m_SetNameToSetObj[text];
				if (viewSheetSet != null)
				{
					this.ReSetSheetViewName(this.lBoxViewSheetSet, viewSheetSet);
					this.m_viewSheet.SettingName = text;
					this.ReSetTreeView(this.trViewSheet);
				}
			}
		}

		protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
		{
			int num = 256;
			int num2 = 260;
			if ((msg.Msg == num | msg.Msg == num2) && keyData == Keys.Escape)
			{
				base.Close();
			}
			return false;
		}

		private void comboSheetSet_DropDown(object sender, EventArgs e)
		{
			this.SetComboBoxMaxLength(this.comboSheetSet);
		}

		private ViewSheets m_viewSheet;

		private Document m_doc;

		private Dictionary<string, View> m_SheetNameToView = new Dictionary<string, View>();

		private Dictionary<string, ViewSheetSet> m_SetNameToSetObj = new Dictionary<string, ViewSheetSet>();
	}
}
